import Vue from 'vue'
import Vuex from 'vuex'

//挂载vuex对象
Vue.use(Vuex)

export default new Vuex.Store({
  //存放的状态
  state: {
    token: '',
    username: localStorage.getItem('username') == null ? '' : localStorage.getItem('username'),
    id: '',
    menuList: [],
    authority: 1
  },
  //状态操作的集合类似于methods
  mutations: {
    //初始化
    initMenu: (state) => {
      if (state.token === '' || state.token == undefined || state.authority === 0) return;
      let auth = state.authority
      if (auth === 1 || auth === 2) {
        state.menuList = [
          {path: '/index', navItem: '首页', icon: 'el-icon-s-home'},
          {path: '/labor', navItem: '我的实验室', icon: 'el-icon-s-help'},
          {path: '/library', navItem: '论文库', icon: 'el-icon-s-management'},
          {path: '/space', navItem: '我的', icon: 'el-icon-user-solid'}
        ]
      } else if (auth === 4) {
        state.menuList = [
          {
            iconCls: 'el-icon-s-help',
            name: '实验室管理',
            children: [{
              name: '实验室数据',
              path: '/admin/labor/data',
              icon: 'el-icon-pie-chart'
              },
              {
                name:'实验室成员',
                path:'/admin/labor/list',
                icon:'el-icon-user'
              }
            ]
          },
          {
            iconCls: 'el-icon-s-help',
            name: '论文管理',
            children: [
              {
              name: '论文库',
              path: '/admin/library/library',
              icon: 'el-icon-folder'
              },
              {
                name:'研究方向',
                path:'/admin/library/direction',
                icon:'el-icon-d-caret'
              }
            ]
          },
        ]
      }
    },
    login: (state, user) => {
      state.token = user.token
      state.username = user.username
      state.id = user.id
      state.authority = user.authority
      localStorage.setItem("token", user.token)
      localStorage.setItem("id", user.id)
      localStorage.setItem('username', user.username)
      localStorage.setItem('authority', user.authority)
    },
    clear: (state) => {
      state.token = ''
      state.username = ''
      state.id = ''
      state.authority = 0
      state.menuList = []
      state.dropDownList = []
      localStorage.clear()
    }
  },
  //异步操作
  actions: {},
  //模块化状态管理
  modules: {}
})
